<?php

class CommentsController extends ControllerBase
{
	public function init() {
		Util::app()->theme = ADMIN_THEME;
		parent::init();
		$this->registerFancybox();
	}

	public function registerFancybox(){
		// Add mousewheel plugin (this is optional)
		Util::registerScriptFile(Util::themeUrl() . '/js/fancybox/jquery.mousewheel-3.0.6.pack.js', CClientScript::POS_HEAD);

		// Add fancyBox main JS and CSS files
		Util::registerScriptFile(Util::themeUrl() . '/js/fancybox/jquery.fancybox.js?v=2.0.6');
		Util::registerCssFile(Util::themeUrl().'/js/fancybox/jquery.fancybox.css?v=2.0.6','screen');

		if (isset($_GET['ajax']))
			$this->layout = '//layouts/none';
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'users'=>Util::getSuperMods(),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model = $this->loadModel($id);
		if ($model->highlight_mod == 1) {
			$model->highlight_mod = 0;
			$model->update('highlight_mod');
		}
		$this->render('view',array(
			'model'=>$model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate($id = "", $post_id = "")
	{
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		$model = new Comments;

		if(isset($_POST['content'])) {
			$model->content		=$_POST['content'];
			$model->create_at 	= date("Y-m-d H:i:s");
			$model->user_id 	= Yii::app()->user->id;
			$model->status 		= 1;
			if ($id != "") {
				$parentComment = $this->loadModel($id);
				$model->post_id 	= $parentComment->post_id;
				$model->parent_id 	= $parentComment->parent_id == "" ? $id : $parentComment->parent_id;
			}
			else {
				$model->post_id 	= $post_id;
			}

			$model->save();
		}

		$this->render('create',array(
			'model' => $model));
	}

	/**
	 * Update_status
	 */
	public function actionUpdate_status($id, $returnUrl) {
		// Update for Announcement
		$model = $this->loadModel($id);
		$model->status = ($model->status == 1) ? 0 : 1;
		$model->update('status');

		if (!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array($returnUrl));
		exit;
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id, $returnUrl, $type = "")
	{
		// Update for Announcement
		$model = $this->loadModel($id);
		$model->highlight_mod = 0;
		if ($type == 1) {
			$model->status = ($model->status == 1) ? 0 : 1;
		}
		$model->update('status');

		$this->redirect(array('index', "post_id" => $model->post_id));
		exit;
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$model = $this->loadModel($id);
			$model->deleteAllByAttributes(array("parent_id" => $id));
			$model->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(array('index'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex($post_id)
	{
		// Get info post_id
		$announcement = Announcement::model()->findByPk($post_id);

		$model = new Comments('search');
		$model->unsetAttributes();

		$model->post_id = $post_id;
		if(isset($_GET['Comments'])) {
			$model->attributes=$_GET['Comments'];
		}

		$this->render('index',array(
			'model' => $model,
			'announcement' => $announcement
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Comments::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='comments-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
